import 'package:flutter/material.dart';
import 'package:flutter_app/comics/ComicsContentViewModel.dart';
import 'package:provider/provider.dart';

class ComicsContentPage extends StatefulWidget {
  ComicsContentPage(this.chapterId);

  final String chapterId;

  @override
  State<StatefulWidget> createState() {
    return _ComicsContentPage(chapterId);
  }
}

class _ComicsContentPage extends State<ComicsContentPage> {
  _ComicsContentPage(this.chapterId);

  String chapterId;

  ComicsContentViewModel viewModel = ComicsContentViewModel();

  @override
  void initState() {
    super.initState();
    viewModel.getComicsContent(chapterId);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => viewModel,
      child: Consumer<ComicsContentViewModel>(
        builder: (_, model, __) {
          return Scaffold(
            body: PageView(children: children(model)),
          );
        },
      ),
    );
  }

  List<Widget> children(ComicsContentViewModel model) {
    List<Widget> list = [];
    if(viewModel.data!=null) {
      for (int i = 0; i < viewModel.data.content.length; i++) {
        var x = Container(
          child: Center(
            child: Column(children: [
              Image.network(
                  model.data.content[i].replaceAll(RegExp(r"\s+\b|\b\s"), ""),
                  fit: BoxFit.fill),
              Text("${i+1}/${viewModel.data.content.length+1}",
              style: TextStyle(
                fontSize: 20
              ),)
            ],),
          ),
        );
        list.add(x);
      }
    }
    return list;
  }
}
